﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Acms.SqlDal
{
    public class FeedDal : Base
    {
        public enum Fields
        {
            ID,
            SiteId,
            Name,
            Created
        }
    
        protected override void  ReadRecord(System.Collections.Hashtable table, System.Data.SqlClient.SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.ID, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetBySiteIdAndName(int siteId, string name)
        {
            SqlCommand cmd = CreateCommand("GetBySiteIdAndName");
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, System.Data.SqlDbType.VarChar, 50);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllBySiteId(int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int siteId, string name, DateTime created, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);

            ExecuteCommand(cmd);

        }
    }
}
